$(function(){
	var temp = null;//定义传参的值
	var title = null;//定义窗口标题
	var table2=null;
	layui.use('table',function(){
		var table=layui.table;
		table2=table.render({
			elem:'#demo'
			,toolbar: '#toolbarDemo'
			,url: '../../atsYearMore/selectAll'//数据接口
			,page:true
			,cols:[[
				 {type: 'checkbox'}
				,{field:'pid',title:'ID' ,width:100,style:'display:none'}
				,{field:'pname',title:'计划名称' ,width:100}
                ,{field:'itemName',title:'体育项目名称' ,width:100}
                ,{field:'coachName',title:'教练员姓名' ,width:100}
				,{field:'otherCoach',title:'其他教练员' ,width:100}
				,{field:'planDate',title:'制定时间' ,width:100}
				,{field:'allTask',title:'总任务' ,width:100}
                ,{field:'startYear',title:'开始年份' ,width:100}
                ,{field:'endYear',title:'结束年份' ,width:100}
                ,{field:'file1',title:'附件1' ,width:100}
                ,{field:'file2',title:'附件2' ,width:100}
                ,{field:'file3',title:'附件3' ,width:100}
                ,{field:'creater',title:'创建人' ,width:100}
                ,{field:'creatTime',title:'创建时间' ,width:100}
                ,{field:'isva',title:'是否有效' ,width:100 ,templet:function(d){if(d.isva=='1') return '是';else return '否';}}
                ,{field:'remark',title:'备注' ,width:100}
				,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemo'}
				
			]]
		});
		 $('table.layui-table thead tr th:eq(1)').addClass('layui-hide');//第一列隐藏


        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#creatTime1'
            });
            laydate.render({
                elem: '#creatTime2'
            });
        });
		 
		 
		 $("#seachTable").click(function(){
			 //执行重载
			 table2.reload({
		    	url:'../../atsYearMore/selectAll',
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
                     name: $("#pname").val(),
                     creatTime1: $("#creatTime1").val(),
                     creatTime2: $("#creatTime2").val()
		        }
		      });
		 });
	
		
		//监听头工具栏事件
		  table.on('toolbar(test)', function(obj){
		    var checkStatus = table.checkStatus(obj.config.id)
		    ,data = checkStatus.data; //获取选中的数据
		    switch(obj.event){
		      case 'add':
		    	temp = "add";
		    	title = "新增界面";
		    	addAndUpd(temp,title);
		      break;
		      case 'update':
		        if(data.length === 0){
		          layer.msg('请选择一行');
		        } else if(data.length > 1){
		          layer.msg('只能同时编辑一个');
		        } else {
			        temp = "upd";
				    title = "修改界面";
				    addAndUpd(temp,title,checkStatus.data[0].pid);
		        }
		      break;
		      case 'delete':
		        if(data.length === 0){
		          layer.msg('请选择一行');
		        } else {
                    layer.confirm('真的删除行么', function(index){
                        delById(checkStatus);
                        layer.close(index);
                    });
		        }
		      break;
		    };
		  });
		  
		  //监听行工具事件
		  table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
		    var data = obj.data.pid //获得当前行数据
		    ,layEvent = obj.event; //获得 lay-event 对应的值
		    if(layEvent === 'del'){	
		      layer.confirm('真的删除行么', function(index){
		    	  delById(data);
		    	  layer.close(index);
		      });
		    } else if(layEvent === 'edit'){
		    	temp = "upd";
			    title = "修改界面";
			    addAndUpd(temp,title,data);
		    }
		  });
	});
	

	function delById(checkStatus){
		var id = checkStatus.data;
		var id_array = [];
		if(!isNaN(checkStatus)){
			id_array.push(checkStatus);
		}else{
			for (var i = 0; i < id.length; i++) {
				id_array.push(checkStatus.data[i].pid);
			}
		}
		var url = "../../atsYearMore/Delete";
		var data = {ids:id_array.join(',')};
		$.post(url,data,function(obj){ 
			if(obj.state==1){
                layer.msg(obj.msg);
				table2.reload();
			}else{
				layer.msg(obj.msg);
			}			
		},"json");
		
	}

	function addAndUpd(temp,title,pid){
	   	layer.open({
	        type : 2,
	        title : title,
	        area : [ '97%', '80%' ],
	        fix : false, 
	        content : 'window.html'+'?&type='+temp+'&pid='+pid,
	        end : function() {
	            //dataTable.reloadTable();
	        }
	    });
	}
	
	
});





